
new Vue({
    el:'#app',
    data:
{
    equation:'0',
    isDecimalAdded:false,
    isOperatorAdded:false,
    isStarted:false,
    
},
methods:
{
//定义一个方法控制+，-，*，/
isOperator(character){
return['+','-','×','÷'].indexOf(character) > -1
},
//为加减乘除(+，-，*，/)定义一个函数，方便重复使用
append(character){
//判断输入的字符串类型
if (this.equation==='0' && !this.isOperator(character)){
if(character==='.'){
this.equation +=''+character
this.isDecimalAdded=true
}else{
this.equation =''+character
}

this.isStarted=true

return
}
//第二次判断
if(!this.isOperator(character)){
    if(character ==='.' && this.isDecimalAdded){
        return
    }
    if(character==='.'){
        this.isDecimalAdded=true
        this.isOperatorAdded=true
    }else{
        this.isOperatorAdded=false
    }
    this.equation += ''+character

}
//第三次判断
if(this.isOperator(character) && !this.isOperatorAdded){
    this.equation+= ''+character
    this.isDecimalAdded=false
    this.isOperatorAdded=true
}   
},
//定义等于(=)符号的方法
calculate(){
let result=this.equation.replace(new RegExp('×','g'),'*').replace(new RegExp('÷','g'),'/')
this.equation=parseFloat(eval(result).toFixed(9)).toString()
this.isDecimalAdded=false
this.isOperatorAdded=false

},
//定义正负号(±)的方法
calculateToggle(){
if(this.isOperatorAdded ||!this.isStarted){
    return
}
this.equation=this.equation+'*-1'
this.calculate()
},
//定义百分号(%)的方法
calculatePercentage(){
    if(this.isOperatorAdded ||!this.isStarted){
        return
    }
    this.equation=this.equation+'*0.01'
    this.calculate()
},
//定义置空(AC)的方法
clear(){
    this.equation='0'
    this.isDecimalAdded=false
    this.isOperatorAdded=false
    this.isStarted=false

}
}
})